$(document).ready(
				function() {
					var contextPath = document.location.pathname; 
					var index =contextPath.substr(1).indexOf("/"); 
					contextPath = contextPath.substr(1,index); 
					var basePath="/"+contextPath;
					var totalCount = $("#totalCount").val();
					var pSize=$("#pageSize").val();;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					$("#orderDetails").on('hidden.bs.modal', function() {
						$(this).removeData("bs.modal");
					});

					$("#optBtn")
							.delegate(
									'#saveBtn','click',
									function() {
										
										$("#orderDetail").attr("method","post");
										
										var orderDetailId = $("#orderDetailId").val();
										
											if (orderDetailId =="") {
												alert("save::"+orderDetailId);
												 $("#orderDetail").attr("action","save.do");
											} else {
												alert("update::"+orderDetailId);
												 $("#orderDetail").attr("action","update.do");
											}
											$("#orderDetail").submit();
										
									});

					$('#pagination2').jqPaginator(
									{
										totalCounts:totalCount,
										pageSize:pSize,
										visiblePages : pageCount,
										currentPage : 1,
										    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											$("#pageIndex").val(num);
											var queryUrl = "queryByPage.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex :num,
															pageSize : pSize
														},
														cache : false,
														success : function(data) {

															
															$("#orderDetailList tr:not(:first)").remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var orderDetail = results[i];
																
																var rowContent = "<tr>"
																		+ "<td><input name='orderDetailId' class='checkbox' type='checkbox' value='"
																		+ orderDetail.id
																		+ "'/></td>"
																		+ "<td>"
																		+ orderDetail.orderId
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.giftId
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.giftName
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.price
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.total
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.summery
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.creator
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.createTime
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.confirmer
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.confirmTime
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.editTime
																		+ "</td>"
																		+ "<td>"
																		+ orderDetail.editor
																		+ "</td>";
																rowContent = rowContent
																		+ "<td><button name='editBtn' objId='"
																		+ orderDetail.id
																		+ "' class='btn btn-sm-block'>修改</button></td>"
																		+ "</tr>";
																$("#orderDetailList")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#orderDetailList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='orderDetailId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#orderDetailList").delegate('[name="editBtn"]', 'click', function() {
						var orderDetailId = $(this).attr("objId");
						alert("orderDetailId:::"+orderDetailId);
						var editUrl = "show.do?orderDetailId=" + orderDetailId;
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								id : orderDetailId
							},
							cache : false,

							success : function(data) {
								loadOrderDetail(data);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});

					});

					$("#tools")
							.delegate(
									'#deleteBtn',
									'click',
									function() {
										delChk = $('input[name="orderDetailId"]:checked');

										if (delChk.length > 0) {
											if (confirm("Are you sure delete the record?")) {

												var ids = "";
												var delUrl = "delete.do";
                                                var idArr=new Array();
                                               
                                                
                                                for (var i = 0; i < delChk.length; i++) {
                                                	idArr.push(delChk.get(i).value);
                                           
											}
												
												$.ajax({
															url : delUrl,
															type: "POST",
															data : {
																ids : idArr.toString()
															},
															cache : false,

															success : function(
																	html) {
																alert("Delete successfully");
																refresh();
															},
															error : function(
																	html) {
																alert("Delete failly");
																refresh();
															}
														});
											} else {
												return;
											}
										} else {
											alert("Please select the record to delete");
											return;
										}
									});
					
					
					$("#tools").delegate('#addOrderDetail', 'click', function() {
						$("#orderDetail input").each(function() {
							
								
								$(this).prop("value", null);
								$(this).prop("readonly", false);
							
							

						});
						
						$("#orderDetail textarea").each(function() {
							$(this).prop("value", null);

						});
						$("#orderDetailInfor").modal('show');

					});
					

				});





function loadOrderDetail(orderDetail) {
	$("#orderDetailId").val(orderDetail.id);
	$("#orderId").val(orderDetail.orderId);
	$("#giftId").val(orderDetail.giftId);
	$("#giftName").val(orderDetail.giftName);
	$("#price").val(orderDetail.price);
	$("#total").val(orderDetail.total);
	$("#summery").val(orderDetail.summery);
	$("#creator").val(orderDetail.creator);
	$("#createTime").val(orderDetail.createTime);
	$("#confirmer").val(orderDetail.confirmer);
	$("#confirmTime").val(orderDetail.confirmTime);
	$("#editTime").val(orderDetail.editTime);
	$("#editor").val(orderDetail.editor);
	$("#note").val(orderDetail.note);
	$("#orderDetailInfor").modal('show');
}

function vaildate(form) {
	return true;
}

function refresh() {
	window.location.reload();
}
